package cn.edu.hbpu.controller;

import cn.edu.hbpu.pojo.*;
import cn.edu.hbpu.service.ReAttendService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author yuwenteng
 * @Date 2017/9/22 11:10
 * @Description 完成补签工作流任务
 */
@Controller
@RequestMapping("reattend")
public class ReAttendController {

    @Autowired
    private ReAttendService reAttendService;

    /**
     * @Author yuwenteng
     * @Date 2017/9/22 22:36
     * @Description 根据学生姓名查询数据库考勤补签数据列表
     */
    @RequestMapping("/findReAttendInfo")
    public String findReAttendInfo(Model model, HttpSession session) {
        StudentCustom studentCustom = (StudentCustom) session.getAttribute("stuInfo");
        List<ReAttend> reAttendList = reAttendService.findReAttendInfo(studentCustom.getStuName());
        model.addAttribute("reAttendList", reAttendList);
        return "reAttend";
    }

    /**
     * @Author yuwenteng
     * @Date 2017/9/25 19:29
     * @Description 开启activiti工作流，完成补签任务信息
     */
    @RequestMapping("/commitReAttendInfo")
    @ResponseBody
    public String commitReAttendInfo(ReAttend reAttend, HttpSession session) {
        StudentCustom studentCustom = (StudentCustom) session.getAttribute("stuInfo");
        //通过session拿到学生信息设置reattend对象属性
        reAttend.setCurrentHandler(studentCustom.getTeacherName());
        reAttendService.commitReAttendInfo(reAttend);
        return "success";
    }

    /**
     * @Author yuwenteng
     * @Date 2017/9/28 12:12
     * @Description 根据任务办理人姓名查询当前正在审批的补签记录
     */
    @RequestMapping("/findReAttendProcessed")
    public String findReAttendProcessed(HttpSession session, Model model) {
        StudentCustom studentCustom = (StudentCustom) session.getAttribute("stuInfo");
        List<ReAttend> reAttendList = reAttendService.findReAttendProcessed(studentCustom.getStuName());
        model.addAttribute("reAttendList", reAttendList);
        return "reAttendTask";
    }

    /**
     * @Author yuwenteng
     * @Date 2017/9/26 13:02
     * @Description 补签任务处理人查询补签任务信息
     */
    @RequiresRoles("main_teacher")
    @RequestMapping("/queryTaskByHandler")
    public String queryTaskByHandler(HttpSession session, Model model) {
        Teacher teacher = (Teacher) session.getAttribute("teaInfo");
        List<ReAttendCustom> reAttendList = reAttendService.queryTaskList(teacher.getTeacherName());
        model.addAttribute("reAttendList", reAttendList);
        return "teacherApprove";
    }

    /**
     * @Author yuwenteng
     * @Date 2017/9/30 11:25
     * @Description 查询当前办理人处理的任务总数
     */
    @RequestMapping("/findCountOfTask")
    @ResponseBody
    public Integer findCountOfTask(HttpSession session) {
        //通过session获取当前用户信息
        Teacher teacher = (Teacher) session.getAttribute("teaInfo");
        Integer count = null;
        if (teacher != null) {
            count = reAttendService.findCountOfTask(teacher.getTeacherName());
        }
        return count;
    }

    /**
     * @Author yuwenteng
     * @Date 2017/9/28 15:46
     * @Description 提交补签处理人的任务
     */
    @RequestMapping("/completeTeacherTask")
    @ResponseBody
    public String completeTeacherTask(ReAttendCustom reAttendCustom) {
        reAttendService.completeTeacherTask(reAttendCustom);
        return "success";
    }
}
